package com.ujs.eduservice.controller.admin;


import com.ujs.commonutils.constant.R;
import com.ujs.eduservice.entity.EduChapter;
import com.ujs.eduservice.entity.course.ChapterVo;
import com.ujs.eduservice.service.EduChapterService;
import com.ujs.servicebase.exception.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-08-07
 */
@RestController
@RequestMapping("/eduservice/chapter")
@Api(tags = "章节管理api")
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    @ApiOperation("根据课程id，返回章节小结")
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable("courseId") String courseId) {
        try {
            List<ChapterVo> list = chapterService.getChapterVideo(courseId);
            return R.ok().data("allChapterList", list);
        } catch (GuliException e) {
            e.printStackTrace();
            return R.error().message(e.getMsg());
        }
    }

    @ApiOperation("添加章节")
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        try {
            chapterService.save(eduChapter);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("添加失败");
        }
    }

    @ApiOperation("根据章节id返回章节信息")
    @GetMapping("/getChapter/{chapterId}")
    public R getChapter(@PathVariable("chapterId") Long chapterId) {
        EduChapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", chapter);
    }

    @ApiOperation("修改章节")
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    @ApiOperation("删除章节")
    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable("chapterId") String chapterId) {
        try {
            chapterService.deleteChapter(chapterId);
            return R.ok();
        } catch (GuliException e) {
            e.printStackTrace();
            return R.error().message(e.getMsg());
        }
    }
}

